#include "./node.h"
#include "../math/vec3.h"
#include "../math/quat.h"
#include "../renderer/model.h"
#include "../physics/collider-base.h"
#include "../physics/collider-box.h"
#include "../physics/collider-sphere.h"
#include "../log.h"

Node::Node(std::string id) : _pos(nullptr), _scale(nullptr), _rotation(nullptr), _collider(nullptr), _model(nullptr)
{
    this->_id = id;
    this->_pos = new Vec3(0, 0, 0);
    this->_scale = new Vec3(1, 1, 1);
    this->_rotation = new Quat();
}
std::string Node::getID()
{
    return this->_id;
}
void Node::setPosition(float x, float y, float z)
{
    this->_pos->set(x, y, z);
    if (this->_collider != nullptr)
    {
        this->_collider->addRef();
    }
}
void Node::setScale(float x, float y, float z)
{
    this->_scale->set(x, y, z);
    if (this->_collider != nullptr)
    {
        this->_collider->addRef();
    }
}
void Node::setRotation(float x, float y, float z, float w)
{
    this->_rotation->set(x, y, z, w);
    if (this->_collider != nullptr)
    {
        this->_collider->addRef();
    }
}
Vec3 *Node::getPosition()
{
    return this->_pos;
}
Vec3 *Node::getScale()
{
    return this->_scale;
}
Quat *Node::getRotation()
{
    return this->_rotation;
}

// void Node::initModel()
// {
//     this->_model = new Model();
// }

ColliderBase *Node::addBoxCollider()
{
    this->_collider = new ColliderBox(this);
    Log::print((this->getID()) + "      " + "this创建box=" + std::to_string(reinterpret_cast<uintptr_t>(this->_collider)));
    return this->_collider;
}

ColliderBase *Node::addSphereCollider()
{
    this->_collider = new ColliderSphere(this);
    return this->_collider;
}
ColliderBase *Node::getCollider()
{
    if (this->_collider != nullptr)
    {
        return this->_collider;
    }
    throw std::out_of_range("Node  UUID " + this->_id + " not found physics collider");
}
void Node::delCollider()
{
    if (this->_collider != nullptr)
    {
        this->_collider->destroy();
    }
    delete this->_collider;
    this->_collider = nullptr;
}

void Node::destroy()
{
    delete this->_pos;
    this->_pos = nullptr;
    delete this->_scale;
    this->_scale = nullptr;
    delete this->_rotation;
    this->_rotation = nullptr;
    delete this->_model;
    this->_model = nullptr;
    this->delCollider();
}

Node::~Node()
{
}
